package com.diandian.backend.kuaishou.life.exchange.controller;

import com.diandian.backend.kuaishou.life.exchange.service.BusinessTokenGenerateService;
import com.diandian.backend.kuaishou.life.exchange.service.BusinessTokenGetService;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/token")
public class TokenController {

    private static final Logger logger = LoggerFactory.getLogger(TokenController.class);

    @Autowired
    private BusinessTokenGetService businessTokenGetService;

    @Autowired
    private BusinessTokenGenerateService businessTokenGenerateService;

    /**
     * 获取token
     */
    @GetMapping( "/get")
    public BaseResult getToken(@RequestHeader(name = "appKey", required = false) String appKey,
                               @RequestHeader(name = "appSecret", required = false) String appSecret,
                               @RequestHeader(name = "merchantId", required = false) String merchantId) {
        String token = businessTokenGetService.getToken(appKey, appSecret, merchantId);
        return new BaseResult(0, "success", token);
    }

    /**
     * 使用code生成token
     * 说明：在商家主账号绑定时调用
     * @param appKey
     * @param appSecret
     * @param code
     * @param state 必须是userId-merchantId-appKey的格式
     * @return
     */
    @GetMapping( "/generate")
    public BaseResult generateToken(@RequestHeader(name = "appKey") String appKey,
                                    @RequestHeader(name = "appSecret") String appSecret,
                                    @RequestParam(name = "code") String code,
                                    @RequestParam(name = "state") String state) {
        businessTokenGenerateService.generate(appKey, appSecret, code, state);
        return new BaseResult(0, "success", null);
    }

}
